package cn.edu.scau.dormitory_management_system.controller;

import java.util.Arrays;
import java.util.Map;

import cn.edu.scau.dormitory_management_system.entity.MessageboardEntity;
import cn.edu.scau.dormitory_management_system.service.MessageboardService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 
 *
 * @author wanglingfu
 * @email 1006340605@qq.com
 * @date 2021-08-02 14:35:10
 */
@RestController
@RequestMapping("/admin/question/messageboard")
public class MessageboardController {
    @Autowired
    private MessageboardService messageboardService;

    //查看所有已发布的公告信息，权限是所有登录用户(后勤管理员和学生)都可查看公告
    @GetMapping("/selectallmessage")
    public Object getMessage(@RequestParam(defaultValue = "1") int current,
                             int size){
        JSONObject object = new JSONObject();
        IPage<MessageboardEntity> page=new Page<>(current,size);
        QueryWrapper<MessageboardEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("me_id");
        page = messageboardService.page(page, wrapper);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无公告数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }


    //后勤管理员发布，删除公告（此处注意公告的长度，最大长度是255个中文字符。）

    //发布公告，插入一条公告记录
    @PostMapping("/insertMessage")
    public Object insertMessage(@RequestBody MessageboardEntity messageboardEntity){
        JSONObject object = new JSONObject();
        boolean flag = messageboardService.insertMessage(messageboardEntity);
        if (flag) {
            object.put("code", 200);
            object.put("msg", "操作成功");
        } else {
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    //根据id删除公告
    @PostMapping("/deleteMessage")
    public Object deleteMessageById(@RequestParam Integer meId){
        JSONObject object = new JSONObject();
        boolean flag = messageboardService.deleteMessage(meId);
        if (flag) {
            object.put("code", 200);
            object.put("msg", "操作成功");
        } else {
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }
}